//
//  LocPickView.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/15.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "LocPickView.h"
#import "config.h"


@interface LocPickView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (strong, nonatomic) UIView *blckView;
@property (nonatomic,strong) NSArray *data;
@property (nonatomic,strong)NSArray *shenArray,*shiArray,*quArray;;

-(void)handleSingleTap:(UITapGestureRecognizer *)sender;

@end


@implementation LocPickView
@synthesize data;

+(instancetype)creadInstance{
    
    LocPickView *view = [[[NSBundle mainBundle]loadNibNamed:@"LocPickView" owner:nil options:nil]lastObject];
    view.pickView.delegate = view;
    view.pickView.dataSource =view;
    return  view;
}

-(void)showInView:(UIView*)view{
    
    
    
    if (data.count==0) {
        NSString *filename = [[NSBundle mainBundle] pathForResource:@"Area.plist" ofType:nil];
        
        NSLog(@"FILE = %@",filename);
        data =  [NSArray arrayWithContentsOfFile:filename];
    }
    
    if (!self.blckView) {
        self.blckView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
        [self.blckView setBackgroundColor:[UIColor blackColor]];
        UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [self.blckView addGestureRecognizer:singleTap];
    }
    
    
    
    CGRect buf = self.frame;
    buf.size.width = view.frame.size.width;
    buf.origin.y = view.frame.size.height;
    [self setFrame:buf];
    
    [self.blckView setAlpha:0];
    
    buf.origin.y = view.frame.size.height - buf.size.height;
    [view addSubview:self.blckView];
    [view addSubview:self];
    
    [UIView animateWithDuration:0.2 animations:^{
        
        
        [self.blckView setAlpha:0.6];
        
        [self setFrame:buf];
        
        
    } completion:^(BOOL finished) {
        
    }];
}



-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    
    [self hide];
}



-(void)hide{
    
    CGRect buf = self.frame;
    buf.origin.y += buf.size.height;
    [UIView animateWithDuration:0.2 animations:^{
        
        [self.blckView setAlpha:0];
        
        [self setFrame:buf];
        
    } completion:^(BOOL finished) {
        
        [self.blckView removeFromSuperview];
        [self removeFromSuperview];
    }];
}



-(NSString*)getAddress{
    
    NSString *str;
    
    NSInteger selectRowInC1 = [self.pickView selectedRowInComponent:0];
    NSInteger selectRowInC2 = [self.pickView selectedRowInComponent:1];
    
    NSDictionary *shenDic = [data objectAtIndex:selectRowInC1];
    str = [shenDic objectForKey:@"stateName"];
    
    NSArray *shiArray  = [shenDic objectForKey:@"city"];
    NSDictionary *shiDic = [shiArray objectAtIndex:selectRowInC2];
    str = [NSString stringWithFormat:@"%@-%@",str,[shiDic objectForKey:@"areaName"]];
    
    return str;
}

-(NSString *)getStateName;
{
    NSInteger selectRowInC1 = [self.pickView selectedRowInComponent:0];
    
    NSDictionary *shenDic = [data objectAtIndex:selectRowInC1];
    return  [shenDic objectForKey:@"stateName"];
}


-(NSString *)getCityName;
{
    
    NSInteger selectRowInC1 = [self.pickView selectedRowInComponent:0];
    NSInteger selectRowInC2 = [self.pickView selectedRowInComponent:1];
    
    NSDictionary *shenDic = [data objectAtIndex:selectRowInC1];
    
    NSArray *shiArray  = [shenDic objectForKey:@"city"];
    NSDictionary *shiDic = [shiArray objectAtIndex:selectRowInC2];
    return  [shiDic objectForKey:@"areaName"];
}

-(NSString*)getCityCode{
    
    NSInteger selectRowInC1 = [self.pickView selectedRowInComponent:0];
    NSInteger selectRowInC2 = [self.pickView selectedRowInComponent:1];
    
    NSDictionary *shenDic = [data objectAtIndex:selectRowInC1];
    
    NSArray *shiArray  = [shenDic objectForKey:@"city"];
    NSDictionary *shiDic = [shiArray objectAtIndex:selectRowInC2];
    return  [shiDic objectForKey:@"areaCode"];
}

-(NSString *)getStateCode{
    
    
    NSInteger selectRowInC1 = [self.pickView selectedRowInComponent:0];
    NSDictionary *shenDic = [data objectAtIndex:selectRowInC1];
    
    
    NSString *str = [shenDic objectForKey:@"stateCode"];
    return str;
    
}
#pragma pickVie delegate


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 2;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    
    if (component==0) {
        return data.count;
        
    }else if (component==1){
        
        NSInteger selectRowInC1 = [pickerView selectedRowInComponent:0];
        NSDictionary *shenDic = [data objectAtIndex:selectRowInC1];
        NSArray *shiArray  = [shenDic objectForKey:@"city"];
        return shiArray.count;
        
    }else{
        return 0;
    }
}



- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    
    return MAIN_SCREEN_bounds.size.width/2;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (component==0) {
        
        NSDictionary *shenDic = [data objectAtIndex:row];
        return [shenDic objectForKey:@"stateName"];
        
    }else if (component==1){
        
        NSInteger selectRowInC1 = [pickerView selectedRowInComponent:0];
        
        NSDictionary *shenDic = [data objectAtIndex:selectRowInC1];
        NSArray *shiArray = [shenDic objectForKey:@"city"];
        
        NSDictionary *shiDic = [shiArray objectAtIndex:row];
        
        
        return [shiDic objectForKey:@"areaName"];
        
    }else{
        return @"title";
    }
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    
    switch (component) {
        case 0:
            
            [pickerView reloadComponent:1];
            
            break;
      
        default:
            break;
    }
}


-(Boolean)isShow{
    
    return [self superview]!=nil;
}


@end
